﻿using System;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace PMIS.UI
{
	/// <summary>
	/// 所有Dockform的基础页
	/// </summary>
	public 	class BaseDockForm : DockContent
	{
		#region 常用委托
		/// <summary>
		/// 无参数委托
		/// </summary>
		public delegate void Action();
		#endregion

		#region 常用对话框
		/// <summary>
		/// 显示普通信息提示框
		/// </summary>
		/// <param name="message">内容</param>
		protected void ShowMessageBox(string message)
		{
			ShowMessageBox("系统信息", message);
		}
		/// <summary>
		/// 显示常见信息对话框
		/// </summary>
		/// <param name="title">标题</param>
		/// <param name="message">内容</param>
		protected void ShowMessageBox(string title, string message)
		{
			MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
		}
		/// <summary>
		/// 显示错误提示框
		/// </summary>
		/// <param name="message">内容</param>
		protected void ShowErrorMessage(string message)
		{
			ShowErrorMessage("系统错误", message);
		}
		/// <summary>
		/// 显示错误信息
		/// </summary>
		/// <param name="title"></param>
		/// <param name="message"></param>
		protected void ShowErrorMessage(string title, string message)
		{
			MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		/// <summary>
		/// 内容
		/// </summary>
		/// <param name="message"></param>
		protected void ShowSuccessMessage(string message)
		{
			ShowSuccessMessage("系统提示", message);
		}
		/// <summary>
		/// 显示成功信息
		/// </summary>
		/// <param name="title">标题</param>
		/// <param name="message">内容</param>
		protected void ShowSuccessMessage(string title, string message)
		{
			MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
		}
		/// <summary>
		/// 显示异常信息
		/// </summary>
		/// <param name="ex">异常</param>
		protected void ShowExceptionMessage(Exception ex)
		{
			//	显示提示信息
			MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
			//	将异常写入数据库
			//BL_ExceptionOperation.WriteException(ex);
		}
		/// <summary>
		/// 显示选择对话框
		/// </summary>
		/// <param name="message">内容</param>
		/// <returns>选择结果</returns>
		protected DialogResult ShowYesNoMessage(string message)
		{
			return ShowYesNoMessage("选择", message);
		}
		/// <summary>
		/// 显示选择对话框
		/// </summary>
		/// <param name="title">标题</param>
		/// <param name="message">内容</param>
		/// <returns>选择结果</returns>
		protected DialogResult ShowYesNoMessage(string title, string message)
		{
			//	默认焦点在Yes上
			return ShowYesNoMessage(title, message, MessageBoxDefaultButton.Button1);
		}
		/// <summary>
		/// 显示选择对话框
		/// </summary>
		/// <param name="message">内容</param>
		/// <param name="defaultbuttton">缺省焦点按钮</param>
		/// <returns>选择结果</returns>
		protected DialogResult ShowYesNoMessage(string message, MessageBoxDefaultButton defaultbuttton)
		{
			return ShowYesNoMessage("选择", message, defaultbuttton);
		}
		/// <summary>
		/// 显示选择对话框
		/// </summary>
		/// <param name="title">标题</param>
		/// <param name="message">内容</param>
		/// <param name="defaultbuttton">缺省焦点按钮</param>
		/// <returns>选择结果</returns>
		protected DialogResult ShowYesNoMessage(string title, string message, MessageBoxDefaultButton defaultbuttton)
		{
			return MessageBox.Show(message, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, defaultbuttton);
		}
		#endregion

		#region 权限判断
		/// <summary>
		/// 遇到没有权限时的操作
		/// </summary>
		protected enum NoAuthorityAction
		{
			显示提示框,
			什么都不做
		}
		/// <summary>
		/// 权限判断
		/// </summary>
		/// <param name="strModuleName">模块名称</param>
		/// <returns>是否具有权限</returns>
		protected bool CheckAuthority(string strModuleName)
		{
			return CheckAuthority(strModuleName, NoAuthorityAction.什么都不做);
		}
		/// <summary>
		/// 权限判断
		/// </summary>
		/// <param name="strModuleName">模块名称</param>
		/// <param name="action">遇到没有权限时的操作</param>
		/// <returns>是否具有权限</returns>
		protected bool CheckAuthority(string strModuleName, NoAuthorityAction action)
		{
			return true;
		}
		/// <summary>
		/// 权限判断
		/// </summary>
		/// <param name="strModuleName">模块名称</param>
		/// <param name="strFunctionName">功能名称</param>
		/// <returns>是否具有权限</returns>
		protected bool CheckAuthority(string strModuleName, string strFunctionName)
		{
			return CheckAuthority(strModuleName, strFunctionName, NoAuthorityAction.什么都不做);
		}
		/// <summary>
		/// 权限判断
		/// </summary>
		/// <param name="strModuleName">模块名称</param>
		/// <param name="strFunctionName">功能名称</param>
		/// <param name="action">遇到没有权限时的操作</param>
		/// <returns>是否具有权限</returns>
		protected bool CheckAuthority(string strModuleName, string strFunctionName, NoAuthorityAction action)
		{
			return true;
		}
		#endregion

		#region 其它
		/// <summary>
		/// 获取指定数值的大写字符串
		/// </summary>
		/// <param name="value">数值</param>
		/// <returns></returns>
		protected string ConvertDecimalToChineseNumber(decimal value)
		{
			string strChineseNumbers = "零壹贰叁肆伍陆柒捌玖";			//	0-9所对应的汉字
			string strChineseUnit = "万仟佰拾亿仟佰拾万仟佰拾元角分";	//	数字位所对应的汉字
			string str3 = string.Empty;									//	从原num值中取出的值
			string str4 = string.Empty;									//	数字的字符串形式
			string result = string.Empty;								//	人民币大写金额形式
			int j;														//	num的值乘以100的字符串长度
			string ch1 = string.Empty;									//	数字的汉语读法
			string ch2 = string.Empty;									//	数字位的汉字读法
			int nzero = 0;												//	用来计算连续的零值是几个
			int temp;													//	从原num值中取出的值

			value = Math.Round(Math.Abs(value), 2);						//	将num取绝对值并四舍五入取2位小数
			str4 = ((long)(value * 100)).ToString();					//	将num乘100并转换成字符串形式
			j = str4.Length;											//	找出最高位
			if (j > 15)
			{
				return "溢出";
			}
			strChineseUnit = strChineseUnit.Substring(15 - j);                      //取出对应位数的str2的值。如：200.55,j为5所以str2=佰拾元角分

			//循环取出每一位需要转换的值
			for (int i = 0; i < j; i++)
			{
				str3 = str4.Substring(i, 1);                    //取出需转换的某一位的值
				temp = Convert.ToInt32(str3);                   //转换为数字         
				if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
				{
					//当所取位数不为元、万、亿、万亿上的数字时
					if (str3 == "0")
					{
						ch1 = "";
						ch2 = "";
						nzero = nzero + 1;
					}
					else
					{
						if (str3 != "0" && nzero != 0)
						{
							ch1 = "零" + strChineseNumbers.Substring(temp * 1, 1);
							ch2 = strChineseUnit.Substring(i, 1);
							nzero = 0;
						}
						else
						{
							ch1 = strChineseNumbers.Substring(temp * 1, 1);
							ch2 = strChineseUnit.Substring(i, 1);
							nzero = 0;
						}
					}
				}
				else
				{
					//该位是万亿，亿，万，元位等关键位
					if (str3 != "0" && nzero != 0)
					{
						ch1 = "零" + strChineseNumbers.Substring(temp * 1, 1);
						ch2 = strChineseUnit.Substring(i, 1);
						nzero = 0;
					}
					else
					{
						if (str3 != "0" && nzero == 0)
						{
							ch1 = strChineseNumbers.Substring(temp * 1, 1);
							ch2 = strChineseUnit.Substring(i, 1);
							nzero = 0;
						}
						else
						{
							if (str3 == "0" && nzero >= 3)
							{
								ch1 = "";
								ch2 = "";
								nzero = nzero + 1;
							}
							else
							{
								if (j >= 11)
								{
									ch1 = "";
									nzero = nzero + 1;
								}
								else
								{
									ch1 = "";
									ch2 = strChineseUnit.Substring(i, 1);
									nzero = nzero + 1;
								}
							}
						}
					}
				}
				if (i == (j - 11) || i == (j - 3))
				{
					//如果该位是亿位或元位，则必须写上
					ch2 = strChineseUnit.Substring(i, 1);
				}
				result = result + ch1 + ch2;
				if (i == j - 1 && str3 == "0")
				{
					//最后一位（分）为0时，加上“整”
					result = result + '整';
				}
			}
			if (value == 0)
			{
				result = "零元整";
			}
			return result;
		}
		#endregion
	}
}